package remote.service;

import client.common.dto.AddRoleDto;
import client.common.result.Result;
import client.common.vo.RoleVO;
import client.common.vo.UserVO;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import remote.common.constant.UrlConstant;
import remote.common.storage.TokenStorage;

import java.util.List;

/**
 * 权限服务接口
 */
public interface RoleService {
    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    default Result<List<RoleVO>> getRoleList() {
        String resultStr = HttpRequest.get(UrlConstant.BASE_URL + "/role/roleList")
                .header("Authorization", TokenStorage.getToken())
                .execute().body();
        System.out.println("resultStr = " + resultStr);
        return JSON.parseObject(resultStr, new TypeReference<Result<List<RoleVO>>>() {
        });
    }
    
    /**
     * 根据角色id获取用户列表
     */
    default Result<List<UserVO>> getUserListByRoleId(Integer roleId) {
        String resultStr = HttpRequest.get(UrlConstant.BASE_URL + "/role/userListByRoleId?roleId=" + roleId)
                .header("Authorization", TokenStorage.getToken())
                .execute().body();
        System.out.println("resultStr = " + resultStr);
        return JSON.parseObject(resultStr, new TypeReference<Result<List<UserVO>>>() {
        });
    }
    
    /**
     * 添加角色
     */
    default Result<String> addRole(AddRoleDto addRoleDto) {
        String resultStr = HttpRequest.post(UrlConstant.BASE_URL + "/role/addRole")
                .header("Authorization", TokenStorage.getToken())
                .body(JSON.toJSONString(addRoleDto))
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, new TypeReference<Result<String>>() {
        });
    }
    
    /**
     * 删除角色
     */
    default Result<String> deleteRole(Integer roleId) {
        String resultStr = HttpRequest.delete(UrlConstant.BASE_URL + "/role/deleteRole?roleId=" + roleId)
                .header("Authorization", TokenStorage.getToken())
                .execute().body();
        System.out.println(resultStr);
        return JSON.parseObject(resultStr, new TypeReference<Result<String>>() {
        });
    }
}
